using mesan.crosscutting.IoC;
using Moq;
using StructureMap.Configuration.DSL;
using TechTalk.SpecFlow;

namespace mesan.core.specifications
{
	public class BaseSteps
	{
		protected static void PutDiContainerIntoContext(Registry registry = null)
		{
			if(registry == null)
				registry = new MockedEndpointsRegistry();

			var diContainer = new StructureMapContainer(registry);
			ScenarioContext.Current.Set<IDiContainer>(diContainer);
		} 

		protected IDiContainer DiContainer
		{
			get { return ScenarioContext.Current.Get<IDiContainer>(); }
		}

		protected Mock<T> GetMockFor<T>() where T : class
		{
			var instance = DiContainer.Get<T>();
			return Mock.Get(instance);
		}

		protected void SetContextValue<T>(T value, string identifier = "")
		{
			if(string.IsNullOrEmpty(identifier))
			{
				ScenarioContext.Current.Set(value);
				return;
			}
			ScenarioContext.Current.Set(value, identifier);
		}

		protected  T GetContextValue<T>(string identifier = "")
		{
			if (string.IsNullOrEmpty(identifier))
				return ScenarioContext.Current.Get<T>();

			return ScenarioContext.Current.Get<T>(identifier);
		}
	}
}
